﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SysLicense
    {
        public int Add(KeRui.Model.SysLicense model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Domain", SqlDbType.VarChar, 50), new SqlParameter("@ServerIP", SqlDbType.VarChar, 15), new SqlParameter("@LicenseCode", SqlDbType.VarChar), new SqlParameter("@IsActivated", SqlDbType.Int, 4) };
            parameters[0].Value = model.Domain;
            parameters[1].Value = model.ServerIP;
            parameters[2].Value = model.LicenseCode;
            parameters[3].Value = model.IsActivated;
            return DbHelperSQL.RunProcedure("SysLicense_ADD", parameters, out num);
        }

        public int Delete(int LicenseID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LicenseID", SqlDbType.Int, 4) };
            parameters[0].Value = LicenseID;
            return DbHelperSQL.RunProcedure("SysLicense_Delete", parameters, out num);
        }

        public KeRui.Model.SysLicense GetModel(int LicenseID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LicenseID", SqlDbType.Int, 4) };
            parameters[0].Value = LicenseID;
            KeRui.Model.SysLicense license = new KeRui.Model.SysLicense();
            DataSet set = DbHelperSQL.RunProcedure("SysLicense_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["LicenseID"].ToString() != "")
                {
                    license.LicenseID = int.Parse(set.Tables[0].Rows[0]["LicenseID"].ToString());
                }
                license.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                license.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                license.LicenseCode = set.Tables[0].Rows[0]["LicenseCode"].ToString();
                if (set.Tables[0].Rows[0]["IsActivated"].ToString() != "")
                {
                    license.IsActivated = int.Parse(set.Tables[0].Rows[0]["IsActivated"].ToString());
                }
                if (set.Tables[0].Rows[0]["ActivateTime"].ToString() != "")
                {
                    license.ActivateTime = DateTime.Parse(set.Tables[0].Rows[0]["ActivateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    license.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return license;
            }
            return null;
        }

        public KeRui.Model.SysLicense GetModel(string Domain, string ServerIP)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Domain", SqlDbType.VarChar, 50), new SqlParameter("@ServerIP", SqlDbType.VarChar, 50) };
            parameters[0].Value = Domain;
            parameters[1].Value = ServerIP;
            KeRui.Model.SysLicense license = new KeRui.Model.SysLicense();
            DataSet set = DbHelperSQL.RunProcedure("SysLicense_GetModelbyDomainServerIP", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["LicenseID"].ToString() != "")
                {
                    license.LicenseID = int.Parse(set.Tables[0].Rows[0]["LicenseID"].ToString());
                }
                license.Domain = set.Tables[0].Rows[0]["Domain"].ToString();
                license.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                license.LicenseCode = set.Tables[0].Rows[0]["LicenseCode"].ToString();
                if (set.Tables[0].Rows[0]["IsActivated"].ToString() != "")
                {
                    license.IsActivated = int.Parse(set.Tables[0].Rows[0]["IsActivated"].ToString());
                }
                if (set.Tables[0].Rows[0]["ActivateTime"].ToString() != "")
                {
                    license.ActivateTime = DateTime.Parse(set.Tables[0].Rows[0]["ActivateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    license.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return license;
            }
            return null;
        }

        public int Update(KeRui.Model.SysLicense model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@LicenseID", SqlDbType.Int, 4), new SqlParameter("@Domain", SqlDbType.VarChar, 50), new SqlParameter("@ServerIP", SqlDbType.VarChar, 15), new SqlParameter("@LicenseCode", SqlDbType.VarChar), new SqlParameter("@IsActivated", SqlDbType.Int, 4) };
            parameters[0].Value = model.LicenseID;
            parameters[1].Value = model.Domain;
            parameters[2].Value = model.ServerIP;
            parameters[3].Value = model.LicenseCode;
            parameters[4].Value = model.IsActivated;
            return DbHelperSQL.RunProcedure("SysLicense_Update", parameters, out num);
        }
    }
}

